package com.example.lab_3;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.widget.Toast;


public class RandomNumberService extends Service {
	private RandomNumberThread random_number_thread;
	private Intent random_number_intent;


	@Override
	public IBinder onBind(Intent intent) {
		this.random_number_intent = intent;
		return null;
	}
	
	@Override
	public void onCreate() {
		this.statement("My Service Created");

		this.random_number_thread = new RandomNumberThread(this.random_number_intent ,100,100000);
	}

	@Override
	public void onDestroy() {
		this.statement("My Service Stopped");
		this.random_number_thread.doStop();
	}
	
	@Override
	public void onStart(Intent intent, int startid) 
	{
		this.statement("My Service Started");
		this.random_number_intent = intent;
		this.random_number_thread.setRandomNumberIntent(this.random_number_intent);
		this.random_number_thread.doStart();
		this.startRandomNumberThread();
	}

	 private void startRandomNumberThread(){
	
		if(!random_number_thread.isAlive()){
			if(random_number_thread.getState() != Thread.State.TERMINATED){
			   statement(" BUThread.start ");
			   random_number_thread.start();
			}
		}
	 }
	 
	 private void statement(String statement){
		 Toast.makeText(this, statement, Toast.LENGTH_LONG).show();
	 }
	 

}
